import {Cell, Group, XInput, XTextarea, Datetime} from 'vux'
import {mapGetters, mapActions, mapState, mapMutations} from "vuex"
import {dateFormat, querystring} from 'vux'

const reportID = Math.floor(Math.random() * (10 - 1000 + 1) + 1000);
const draftTxt = {
  d1:"是否确认存为草稿？",
  d2:"存草稿成功",
  d3:"存草稿中..",
};

export default {
  data () {
    return {
      labelT1: '日志类型',
      labelT2: '日志时间',
      labelT3: '发送对象',
      rType: [
        {txt: "日报", value: 1},
        {txt: "周报", value: 2},
        {txt: "月报", value: 3},
      ],
      cType: {txt: "周报", value: 2},
      rTime: "2017-11-12",
      titleV: ""
    }
  },
  methods: {
    init(){
      this._title("编写日志");
      this.getDraft();
    },
    getUrlData(){
      let l = window.location.href;
      let a = querystring.parse(l.substring(l.indexOf("?") + 1));
      this.draftFlag = a.flag && a.flag == 3 ? true : false;
      return a;
    },
    getDraft(){
      let u = this.getUrlData();
      if (this.draftFlag) {
        this.draftsList.forEach((i) => {
          if (i.id == u.id) {
            this.titleV = i.h;
            document.querySelectorAll(".weui-textarea")[0].value = i.c;
          }
        })
      }
    },
    draftSubmit(){
      if (this.draftFlag){
        let id = this.getUrlData().id;
        let a = [];
        this.draftsList.forEach((i) => {
          if (i.id != id) {
            a.push(i);
          }
        });
        this._draftsList(a);
      }
    },
    emptyReportData(){
      document.querySelectorAll(".weui-textarea")[0].value = "";
      this.titleV = "";
    },
    _cType(v){
      this.cType = v;
    },
    change (value) {
      this.rTime = value;
    },
    saveR(){
      this.$confirmY(draftTxt.d1,(v)=>{
        //确认存草稿
        if (v === 1){
          window.vLoading.show(draftTxt.d3);
          let id = this.draftFlag ? this.getUrlData().id : reportID;
          let v = {
            id:Math.floor(Math.random() * (10 - 1000 + 1) + 1000),
            f: this.cType.txt,
            t: dateFormat(new Date(), "YYYY/MM/DD HH:mm"),
            h: this.titleV,
            rp: "收件人1",
            c: document.querySelectorAll(".weui-textarea")[0].value,
            p: "本机"
          };
          let a = this.draftsList;
          if (this.draftFlag){
            a.forEach((i,x)=>{
              if (i.id == id){
                a[x] = v;
              }
            });
          }else {
            a.push(v);
          }
          this._draftsList(a);
          this.emptyReportData();
          setTimeout(() => {
            window.vLoading.hide();
            this.$alertY(draftTxt.d2,()=>{
              this.$router.push({name: "index"});
            });
          }, 1000);
        }
      });
    },
    reportSubmit(){

    },
    addReceivedList(v){
      let a = this.receivedList;
      a.push(v);
      this._receivedList(a);
    },
    submitB(){
      if (this.titleV.trim() === "") {
        window.vToast("请输入日志标题");
        return;
      }
      let rc = document.querySelectorAll(".weui-textarea")[0].value;
      if (rc.trim() === "") {
        window.vToast("请输入日志内容");
        return;
      }
      let v = {
        id: reportID,
        f: this.cType.txt,
        t: dateFormat(new Date(), "YYYY/MM/DD HH:mm"),
        h: this.titleV,
        rp: "收件人1",
        c: rc,
        p: "本机"
      };
      this.addReceivedList(v);
      this.$confirmY("是否确认提交？",(i)=>{
        if (i === 1) {
          this.draftSubmit();
          window.vLoading.show("提交中");
          this.titleV = "";
          document.querySelectorAll(".weui-textarea")[0].value = "";
          setTimeout(() => {
            window.vLoading.hide();
            this.$alertY("提交成功",()=>{
              this.$router.push({name: "inboxDetail", params: {id: v.id}});
              setTimeout(() => {
                this._title("日志详情");
              }, 100)
            })
          }, 1000);
        }
      });
    },
    ...mapActions([
      '_title',
    ]),
    ...mapMutations([
      '_receivedList',
      '_draftsList'
    ])
  },
  computed: {
    ...mapGetters([
      'title',
    ]),
    ...mapState([
      'receivedList',
      'draftsList'
    ]),
  },
  components: {
    Datetime,
    Group,
    XInput,
    XTextarea,
    Cell
  },
  activated(){
    this.init();
  }
}
